package com.wisdudu.blank.ui.setting.system;

import android.databinding.ObservableField;
import android.text.TextUtils;

import com.kelin.mvvmlight.base.ViewModel;
import com.kelin.mvvmlight.command.ReplyCommand;
import com.wisdudu.blank.data.Injection;
import com.wisdudu.blank.data.repo.UserRepo;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.Abs;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.ProgressSubscriber;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.SubscriberOnNextErrorListener;
import com.wisdudu.blank.support.util.ToastUtil;
import com.wisdudu.blank.support.widget.loading.LoadingProgressDialog;

/**
 * Created by ypp on 2016/10/8.
 */
public class FeedBackVM implements ViewModel {
    private UserRepo mUserRepo;
    private FeedBackFragment fragment;
    public ObservableField<String> content = new ObservableField<>();
    public final ObservableField<Integer> etLength = new ObservableField<>();

    public FeedBackVM(FeedBackFragment feedBackFragment) {
        this.fragment = feedBackFragment;
        mUserRepo = Injection.provideUserRepo();
        etLength.set(140);
    }

    public ReplyCommand confirm = new ReplyCommand(() -> {
        fragment.hideKeyboard();
        if (TextUtils.isEmpty(content.get())) {
            ToastUtil.INSTANCE.toast("意见内容不能为空");
            return;
        }
        mUserRepo.feedback(content.get()).compose(fragment.bindToLifecycle())
                .subscribe(new ProgressSubscriber<Abs>(new SubscriberOnNextErrorListener<Abs>() {
                    @Override
                    public void onError(Throwable e, LoadingProgressDialog pd) {
                        ToastUtil.INSTANCE.netError();
                    }

                    @Override
                    public void onNext(Abs abs, LoadingProgressDialog pd) {
                        ToastUtil.INSTANCE.toast(abs.getMessage());
                        if (abs.isSuccess()) {
                            fragment.removeFragment();
                        }
                    }
                }, fragment.mActivity, "正在提交..."));
    });
}
